package com.leetcode.周赛.第231场;

/**
 * @author: xiaomi
 * @date: 2021/3/7
 * @description: 5698. 构成特定和需要添加的最少元素
 * https://leetcode-cn.com/contest/weekly-contest-231/problems/minimum-elements-to-add-to-form-a-given-sum/
 */
public class B_5698_构成特定和需要添加的最少元素 {

    public static void main(String[] args) {
        int[] nums = new int[]{1, -1, 1};
        int limit = 3;
        limit = 1000000;
        int goal = -4;
        goal = -1000000000;
        System.out.println(minElements(nums, limit, goal));
    }

    /**
     * abs
     * int 越界的问题
     *
     * @param nums
     * @param limit
     * @param goal
     * @return
     */
    public static int minElements(int[] nums, int limit, int goal) {
        int len = nums.length;
        //1.求和
        long sum = 0;
        for (int i = 0; i < len; i++) {
            sum += nums[i];
        }
//        sum=100000000000L;
        long dis = 0;
        if (goal > sum) {
            dis = goal - sum;
        } else {
            dis = sum - goal;
        }
        if (dis % limit == 0) {
            return (int)(dis/limit);
        }else {
            return (int)(dis/limit)+1;
        }
//        while (dis > 0) {
//            dis -= limit;
//            count++;
//        }
//        return count;

    }

}
